﻿using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExplorerPlus.AmazonApi.AmazonWebService;

namespace ExplorerPlus.AmazonApi
{
    internal class AmazonKeys
    {
        internal const string AMAZON_ACCESS_KEY_ID = "AKIAILDQBZ44ZD7U5LZQ";
        internal const string AMAZON_SECRET_KEY = "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";
    }

    internal class AmazonUrls
    {
        internal const string AMAZON_CANADA  = "https://webservices.amazon.ca/onca/soap?Service=AWSECommerceService";
        internal const string AMAZON_GERMANY = "https://webservices.amazon.de/onca/soap?Service=AWSECommerceService";
        internal const string AMAZON_FRANCE  = "https://webservices.amazon.fr/onca/soap?Service=AWSECommerceService";
        internal const string AMAZON_JAPAN   = "https://webservices.amazon.co.jp/onca/soap?Service=AWSECommerceService";
        internal const string AMAZON_UK      = "https://webservices.amazon.co.uk/onca/soap?Service=AWSECommerceService";
        internal const string AMAZON_US      = "https://webservices.amazon.com/onca/soap?Service=AWSECommerceService";
    }

    public class AmazonClient
    {
        private static string amazonUrl = AmazonUrls.AMAZON_US;

        public ItemSearchResponse Search(string searchIndex, string title)
        {
            // prepare an ItemSearch request
            // return the results
            return Search(searchIndex, title, "Small");
        }

        public ItemSearchResponse SearchAdvanced(string searchIndex, string title)
        {
            // prepare an ItemSearch request
            // return the results
            return Search(searchIndex, title, "Medium");
        }

        private ItemSearchResponse Search(string searchIndex, string title, string group)
        {
            // prepare an ItemSearch request
            ItemSearchRequest request = new ItemSearchRequest();
            request.SearchIndex = searchIndex; //"Books";
            request.Keywords = title; //"WCF";
            request.ResponseGroup = new string[] { group }; //new string[] { "Large" };
            request.Count = "24";

            // return the results
            return Find(request);
        }

        public ItemLookupResponse FindItemByASIN(string ASIN)
        {
            // prepare an ItemSearch request
            ItemLookupRequest request = new ItemLookupRequest();
            request.IdType = ItemLookupRequestIdType.ASIN;
            request.ItemId = new string[] { ASIN }; 
            request.ResponseGroup = new string[] { "Large" }; //new string[] { "Large" };

            // return the results
            return Find(request);
        }

        private ItemLookupResponse Find(ItemLookupRequest request)
        {
            // create a WCF Amazon ECS client
            AWSECommerceServicePortTypeClient client = InitializeAmazonWebService();

            // prepare an ItemLookup request
            ItemLookup itemLookup = new ItemLookup();
            itemLookup.Request = new ItemLookupRequest[] { request };
            itemLookup.AWSAccessKeyId = AmazonKeys.AMAZON_ACCESS_KEY_ID;

            // issue the ItemSearch request
            ItemLookupResponse response = client.ItemLookup(itemLookup);

            // return the results
            return response;
        }

        private ItemSearchResponse Find(ItemSearchRequest request)
        {
            // create a WCF Amazon ECS client
            AWSECommerceServicePortTypeClient client = InitializeAmazonWebService();

            // prepare an ItemSearch request
            ItemSearch itemSearch = new ItemSearch();
            itemSearch.Request = new ItemSearchRequest[] { request };
            itemSearch.AWSAccessKeyId = AmazonKeys.AMAZON_ACCESS_KEY_ID;

            // issue the ItemSearch request
            ItemSearchResponse response = client.ItemSearch(itemSearch);

            // return the results
            return response;
        }

        public static void SetAmazonLocales(string country)
        {
            switch (country.ToUpper())
            {
                case "CANADA":
                    amazonUrl = AmazonUrls.AMAZON_CANADA;
                    break;

                case "GERMANY":
                    amazonUrl = AmazonUrls.AMAZON_GERMANY;
                    break;

                case "FRANCE":
                    amazonUrl = AmazonUrls.AMAZON_FRANCE;
                    break;

                case "JAPAN":
                    amazonUrl = AmazonUrls.AMAZON_JAPAN;
                    break;

                case "UNITED KINGDOM":
                    amazonUrl = AmazonUrls.AMAZON_UK;
                    break;

                default:
                    amazonUrl = AmazonUrls.AMAZON_US;
                    break;
            }
        }

        private AWSECommerceServicePortTypeClient InitializeAmazonWebService()
        {
            // create a WCF Amazon ECS client
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
            AWSECommerceServicePortTypeClient client = new AWSECommerceServicePortTypeClient(
                binding, new EndpointAddress(amazonUrl));

            // add authentication to the ECS client
            client.ChannelFactory.Endpoint.Behaviors.Add(new AmazonSigningEndpointBehavior());

            return client;
        }

        #region Unused Code
        /*
        public ItemSearchResponse FindItem(string searchIndex, string title)
        {
            // prepare an ItemSearch request
            ItemSearchRequest request = new ItemSearchRequest();
            request.SearchIndex = searchIndex; //"Books";
            request.Keywords = title; //"WCF";
            request.ResponseGroup = new string[] { "Large" }; //new string[] { "Large" };
            request.Count = "1";

            // return the results
            return Find(request);
        }

        public ItemSearchResponse FindBook(string title, string author, string publisher)
        {
            // prepare an ItemSearch request
            ItemSearchRequest request = new ItemSearchRequest();
            request.SearchIndex = "Books";
            request.Title = title;
            request.Author = author;
            request.Publisher = publisher;
            request.ResponseGroup = new string[] { "Large" };
            request.Count = "1";

            // return the results
            return Find(request);
        }

        public ItemSearchResponse FindMovie(string title, string actor, string studio)
        {
            // prepare an ItemSearch request
            ItemSearchRequest request = new ItemSearchRequest();
            request.SearchIndex = "DVD";
            request.Title = title;
            request.Actor = actor;
            request.Publisher = studio;
            request.ResponseGroup = new string[] { "Large" };
            request.Count = "1";

            // return the results
            return Find(request);
        }

        public ItemSearchResponse FindGame(string title, string manufacturer)
        {
            // prepare an ItemSearch request
            ItemSearchRequest request = new ItemSearchRequest();
            request.SearchIndex = "VideoGames";
            request.Title = title;
            request.Manufacturer = manufacturer;
            request.ResponseGroup = new string[] { "Large" };
            request.Count = "1";

            // return the results
            return Find(request);
        }

        public ItemSearchResponse FindMusic(string title, string artist, string manufacturer)
        {
            // prepare an ItemSearch request
            ItemSearchRequest request = new ItemSearchRequest();
            request.SearchIndex = "Music";
            request.Title = title;
            request.Artist = artist;
            request.MusicLabel = manufacturer;
            request.ResponseGroup = new string[] { "Large" };
            request.Count = "1";

            // return the results
            return Find(request);
        }
*/
        #endregion

    }
}
